package com.winning.juc;

import java.util.concurrent.CountDownLatch;

public class TestCountDownLatch {

    public static void main(String[] args) throws InterruptedException {
        long start = System.currentTimeMillis();
        CountDownLatch countDownLatch = new CountDownLatch(5);
        final LatchDemo latchDemo = new LatchDemo(countDownLatch);
        for (int i = 0; i < 5; i++) {
            new Thread(latchDemo).start();
        }

        countDownLatch.await();
        long end = System.currentTimeMillis();

        System.out.println("耗时为：" + (end -start));
 
    }
}

class LatchDemo implements Runnable{

    private CountDownLatch countDownLatch;

    public LatchDemo(CountDownLatch countDownLatch){
        this.countDownLatch = countDownLatch;
    }

    @Override
    public void run() {

        synchronized (this){
            try{
                for (int i = 0; i < 50000; i++) {
                    if((i & 1) == 1){
                        System.out.println(i);
                    }
                }
            }finally {
                countDownLatch.countDown();
            }

        }



    }
}
